package l.l.w.ThinkInJave.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by llw on 2018/3/21/0021.
 */
public class ToStringCheck {

    public static void testToString(User user){
        Class<? extends User> clazz = (Class)user.getClass();
        Override annotation = clazz.getAnnotation(Override.class);
        Annotation[] annotations = clazz.getAnnotations();
        if(clazz.isAnnotationPresent(ToString.class)){
            Field[] fields = clazz.getDeclaredFields();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(clazz.getSimpleName()+"{");
            for(Field f : fields){
                f.setAccessible(true);
                try {
                    stringBuilder.append(f.getName() + "='" + f.get(user) +"', ");
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            stringBuilder.append("}");
            System.out.println(stringBuilder.toString());
        }
    }

    public static void main(String[] args) {
        User user = new User();
        user.setAge(18);
        user.setUserName("li hua");
        user.setPassword("12306");
        user.setSex(false);
        System.out.println(user.toString());
        testToString(user);
    }
}
